/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.pagebeans;

import edu.chl.da.controllerBeans.PackageReservationBean;
import edu.chl.da.ejb.core.Destination;
import edu.chl.da.ejb.ctrl.IManageDestination;
import edu.chl.da.ejb.ctrl.IManageFlight;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Erik
 */
@ManagedBean(name="PackageBoxBean")
@SessionScoped
public class PackageBoxBean {

    private String fromDestination;

    private List<SelectItem> listFromDestinations;

    private PackageReservationBean packageReservation;

    @EJB
    private IManageFlight imf;

    @EJB
    private IManageDestination imd;

    /** Creates a new instance of PackageBoxBean */
    public PackageBoxBean() {

    }

    public void load(){
        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        ValueExpression ex =
           context.getApplication().getExpressionFactory().createValueExpression(elContext,"#{PackageReservationBean}", PackageReservationBean.class);

         this.packageReservation = (PackageReservationBean) ex.getValue(elContext);
    }

    public List<SelectItem> getListFromDestinations() throws ParseException{
        this.listFromDestinations = new ArrayList<SelectItem>();
        for(Destination d : imf.getAllFromDestinationRouteFlights()){
            listFromDestinations.add(new SelectItem(d.getName(),d.getName()));
        }
        return listFromDestinations;
    }

    public String getFromDestination() {
        return fromDestination;
    }

    public void setFromDestination(String fromDestination) {
        this.fromDestination = fromDestination;
    }

    public String nextStep(){
        this.packageReservation.setFromDestination(fromDestination);
        return "/views/reservePackage.jsf?faces-redirect=true";
    }
}
